#include "Button.h"

/*宏定义*/
#define BUTTON1 26
#define BUTTON2 21
#define BUTTON3 12
#define BUTTON4 13

#define BUTTON1_PUSH        1
#define BUTTON2_PUSH        2
#define BUTTON3_PUSH        3
#define BUTTON1_LONG_PUSH   4
#define BUTTON2_LONG_PUSH   5
#define BUTTON3_LONG_PUSH   6
#define BUTTON1_TWO_PUSH    7
#define BUTTON2_TWO_PUSH    8
#define BUTTON3_TWO_PUSH    9

Ticker tickerButton1;
Ticker tickerButton2;
Ticker tickerButton3;

struct Button
{
    const uint8_t PIN;
    bool pressed;
    uint8_t key_flag;
    uint8_t cur_tick;
    Ticker *tick;
    void (*callback)(void);
};

void Button1_tick(void);
void Button2_tick(void);
void Button3_tick(void);

Button button1 = {21, false, 0, 0, &tickerButton1, Button1_tick};
Button button2 = {22, false, 0, 0, &tickerButton2, Button2_tick};
Button button3 = {35, false, 0, 0, &tickerButton3, Button3_tick};

void Button1_tick(void)
{
    if(button1.pressed == true)
    {
        if(digitalRead(button1.PIN))
        {
            button1.cur_tick++;
            if(button1.cur_tick >= 3)
            {
                button1.key_flag = 2;
                button1.tick->detach();
                button1.pressed = false;
            }
        }
        else
        {
            button1.key_flag = 1;
            button1.tick->detach();
            button1.pressed = false;
        }
    }
}

void Button2_tick(void)
{
    if(button2.pressed == true)
    {
        if(digitalRead(button2.PIN))
        {
            button2.cur_tick++;
            if(button2.cur_tick >= 3)
            {
                button2.key_flag = 2;
                button2.tick->detach();
                button2.pressed = false;
            }
        }
        else
        {
            button2.key_flag = 1;
            button2.tick->detach();
            button2.pressed = false;
        }
    }
}

void Button3_tick(void)
{
    if(button3.pressed == true)
    {
        if(digitalRead(button3.PIN))
        {
            button3.cur_tick++;
            if(button3.cur_tick >= 3)
            {
                button3.key_flag = 2;
                button3.tick->detach();
                button3.pressed = false;
            }
        }
        else
        {
            button3.key_flag = 1;
            button3.tick->detach();
            button3.pressed = false;
        }
    }
}

void IRAM_ATTR isr(void *arg)
{
    Button *s = static_cast<Button *>(arg);
    if(digitalRead(s->PIN))
    {
        if(s->pressed == true)
        {
            s->key_flag = 3;
            s->tick->detach();
            s->pressed = false;
        }
        else
        {
            s->pressed = true;
            s->tick->attach_ms(500, s->callback);
        }
    }
    else
    {
        // if(s->pressed == true)
        // {
        //     s->key_flag = 1;
        //     s->pressed = false;
        //     s->tick->detach();
        // }
    }
    
}

void Button_Init(void)
{
    pinMode(button1.PIN, INPUT_PULLUP);
    attachInterruptArg(button1.PIN, isr, &button1, RISING);
    pinMode(button2.PIN, INPUT_PULLUP);
    attachInterruptArg(button2.PIN, isr, &button2, RISING);
    pinMode(button3.PIN, INPUT_PULLUP);
    attachInterruptArg(button3.PIN, isr, &button3, RISING);

    Serial.println("button init success");
}

uint8_t get_button_state(void)
{
    uint8_t flag;

    if (button1.key_flag != 0)
    {
        flag = button1.key_flag;
        button1.key_flag = 0;
        if(flag == 2)
        {
            return BUTTON1_LONG_PUSH;
        }
        else if(flag == 3)
        {
            return BUTTON1_TWO_PUSH;
        }

        return BUTTON1_PUSH;
    }

    if (button2.key_flag != 0)
    {
        flag = button2.key_flag;
        button2.key_flag = 0;
        if(flag == 2)
        {
            return BUTTON2_LONG_PUSH;
        }
        else if(flag == 3)
        {
            return BUTTON2_TWO_PUSH;
        }

        return BUTTON2_PUSH;
    }

    if (button3.key_flag != 0)
    {
        flag = button3.key_flag;
        button3.key_flag = 0;
        if(flag == 2)
        {
            return BUTTON3_LONG_PUSH;
        }
        else if(flag == 3)
        {
            return BUTTON3_TWO_PUSH;
        }

        return BUTTON3_PUSH;
    }

    
    return 0;
}
